package com.exmaple.lemlin.mvp.presenter;

import android.content.Context;

import com.exmaple.lemlin.app.APPIM;
import com.exmaple.lemlin.entity.FriendGood;
import com.exmaple.lemlin.entity.FriendGoodDao;
import com.exmaple.lemlin.mvp.base.BasePresenter;
import com.exmaple.lemlin.mvp.view.IFriendView;
import com.exmaple.lemlin.utils.JsonUtils;
import com.exmaple.lemlin.utils.LogUtils;
import com.exmaple.lemlin.xmpp.XmppConnection;

import org.greenrobot.greendao.query.QueryBuilder;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smackx.search.ReportedData;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import io.reactivex.schedulers.Schedulers;

public class FriendPresenter extends BasePresenter<IFriendView> {
    public FriendPresenter(Context context, IFriendView view) {
        super(context, view);
    }

    public Disposable userSearch(String search) {

        Observable<List<FriendGood>> observable = Observable.just(search).subscribeOn(Schedulers.io())
                .flatMap(new Function<String, ObservableSource<ReportedData.Row>>() {
                    @Override
                    public ObservableSource<ReportedData.Row> apply(String s) throws Exception {
                        return Observable.fromIterable(XmppConnection.getInstance().userSearch(s));
                    }
                })
                .map(new Function<ReportedData.Row, FriendGood>() {
                    @Override
                    public FriendGood apply(ReportedData.Row row) throws Exception {
                        FriendGood friend = new FriendGood();
                        friend.setName(row.getValues(("Username")).get(0).toString());
                        friend.setJid(row.getValues(("jid")).get(0).toString());
                        friend.setSub("node");
                        return friend;
                    }
                })
                .toList()
                .toObservable()
                .observeOn(AndroidSchedulers.mainThread());
        return observable.subscribe(new Consumer<List<FriendGood>>() {
            @Override
            public void accept(List<FriendGood> friendGoods) throws Exception {
                mView.onFriendGoods(friendGoods);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onHint("搜索错误：" + throwable.getMessage());
            }
        });
    }

    public Disposable addCreateFriend(String jidString) {
        Observable<Boolean> observable = Observable.just(jidString).subscribeOn(Schedulers.io())
                .map(new Function<String, Boolean>() {
                    @Override
                    public Boolean apply(String s) throws Exception {
                        return XmppConnection.getInstance().addCreateFriend(s, null, null);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread());
        return observable.subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    mView.onHint("已发送请求,等待对方同意");
                } else {
                    mView.onHint("添加失败");
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onHint("添加错误：" + throwable.getMessage());
            }
        });
    }

    public Disposable subscribedAddCreateFriend(final String jidString) {

        Observable<Boolean> observable = Observable.just(jidString).subscribeOn(Schedulers.io())
                .map(new Function<String, Boolean>() {
                    @Override
                    public Boolean apply(String s) throws Exception {
                        return XmppConnection.getInstance().subscribed(s, Presence.Type.subscribed);
                    }
                })
                .filter(new Predicate<Boolean>() {
                    @Override
                    public boolean test(Boolean aBoolean) throws Exception {
                        if (!aBoolean) {
                            mView.onHint("同意订阅失败");
                        }
                        return aBoolean;
                    }
                })
                .map(new Function<Boolean, Boolean>() {
                    @Override
                    public Boolean apply(Boolean aBoolean) throws Exception {
                        return XmppConnection.getInstance().addCreateFriend(jidString, null, null);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread());
        return observable.subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    mView.onHint("已同意并添加对方为好友");
                    mView.onSuccessAddSub();
                } else {
                    mView.onHint("同意并添加失败");
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onHint("同意并添加错误：" + throwable.getMessage());
            }
        });
    }
}
